package com.example.administrator.diary;

/**
 * Created by Administrator on 2018/6/16.
 */
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import com.example.administrator.diary.DiaryMain;

import com.example.administrator.diary.Fields.DiaryColumns;


public class DiaryEditor extends AppCompatActivity {


    private int FontFlag=1;
    public static int STYLE_ID=R.style.AppTheme;
    public static final String EDIT_DIARY_ACTION
            = "com.example.administrator.diary.DiaryEditor.EDIT_DIARY";
    public static final String INSERT_DIARY_ACTION
            = "com.example.administrator.diary.DiaryEditor.action.INSERT_DIARY";
    private static final String[] PROJECTION
            = new String[] { DiaryColumns._ID, // 0
            DiaryColumns.TITLE, DiaryColumns.BODY, // 1
    };




    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;
    private int mState;
    private Uri mUri;
    private Cursor mCursor;
    private EditText mTitleText;
    private EditText mBodyText;
    private Button confirmButton;


    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = getIntent();
        final String action = intent.getAction();

        setTheme(DiaryMain.theme);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null)
        {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        setContentView(R.layout.diary_edit);
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        confirmButton = (Button) findViewById(R.id.confirm);
        // 关闭软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        mTitleText.setTextSize(getResources().getDimension(R.dimen.smallfont));
        mBodyText.setTextSize(getResources().getDimension(R.dimen.smallfont));

        if (EDIT_DIARY_ACTION.equals(action)) {// 编辑日记
            mState = STATE_EDIT;
            mUri = intent.getData();
            mCursor = managedQuery(mUri, PROJECTION, null, null, null);
            mCursor.moveToFirst();

            mTitleText.setEnabled(false);
            mBodyText.setEnabled(false);
            confirmButton.setVisibility(View.INVISIBLE);
            if(DiaryMain.theme==STYLE_ID)
            {
                mTitleText.setTextColor(Color.rgb(0,0,0));
                mBodyText.setTextColor(Color.rgb(0,0,0));       //Day_BlackText
            }
            else {
                mTitleText.setTextColor(Color.rgb(255,255,255));
                mBodyText.setTextColor(Color.rgb(255,255,255));         //Night_WhiteText
            }

            String title = mCursor.getString(1);
            mTitleText.setTextKeepState(title);
            String body = mCursor.getString(2);
            mBodyText.setTextKeepState(body);
            setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
//            setTitle("编辑日记");

            mTitleText.setEnabled(false);
            mBodyText.setEnabled(false);
            confirmButton.setVisibility(View.INVISIBLE);
        }
        else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记
            mState = STATE_INSERT;
            if(DiaryMain.theme==STYLE_ID)
            {
                mTitleText.setTextColor(Color.rgb(0,0,0));
                mBodyText.setTextColor(Color.rgb(0,0,0));
            }
            else {
                mTitleText.setTextColor(Color.rgb(255,255,255));
                mBodyText.setTextColor(Color.rgb(255,255,255));
            }
            actionBar.hide();
//            setTitle("新建日记");
        }
        else {
            Log.e("Error", "no such action error");
            finish();
            return;
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mState == STATE_INSERT) {
                 insertDiary();
                } else {
                   updateDiary();
                }
                Intent mIntent = new Intent();
                setResult(RESULT_OK, mIntent);
                finish();
            }
        });
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.edit,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                this.finish();
                return true;
            case R.id.action_font:
                if(FontFlag==0)
                {
                    mTitleText.setTextSize(getResources().getDimension(R.dimen.smallfont));
                    mBodyText.setTextSize(getResources().getDimension(R.dimen.smallfont));
                    FontFlag=1;
                }
                else
                {
                    mTitleText.setTextSize(getResources().getDimension(R.dimen.midefont));
                    mBodyText.setTextSize(getResources().getDimension(R.dimen.midefont));
                    FontFlag=0;
                }

                return true;

            case R.id.action_edit:
                //开启可编辑
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                mTitleText.setEnabled(true);
                mBodyText.setEnabled(true);
                confirmButton.setVisibility(View.VISIBLE);
                return true;

            case R.id.action_share:
                Intent intent2=new Intent(Intent.ACTION_SEND);
                intent2.setType("text/*");
                intent2.putExtra(Intent.EXTRA_SUBJECT, "Share");

                String sendtitle = mTitleText.getText().toString();
                String sendbody = mBodyText.getText().toString();
                intent2.putExtra(Intent.EXTRA_TEXT, sendtitle+":\n"+sendbody);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent2, getTitle()));


                return true;


        }
        return super.onOptionsItemSelected(item);
    }
    private void insertDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        if(title.equals(""))
        {
            title="无标题";
        }
        ContentValues values = new ContentValues();
        values.put(Fields.DiaryColumns.CREATED, DiaryContentProvider.getFormateCreatedDate());
        values.put(Fields.DiaryColumns.TITLE, title);
        values.put(Fields.DiaryColumns.BODY, body);
        getContentResolver().insert(Fields.DiaryColumns.CONTENT_URI, values);


    }

    private void updateDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        if(title.equals(""))
        {
            title="无标题";
        }
        ContentValues values = new ContentValues();
        values.put(Fields.DiaryColumns.CREATED, DiaryContentProvider.getFormateCreatedDate());
        values.put(Fields.DiaryColumns.TITLE, title);
        values.put(Fields.DiaryColumns.BODY, body);
        getContentResolver().update(mUri, values,null, null);
    }

}
